<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增人员1')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-employee1-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label">姓名：</label>
                <div class="col-sm-8">
                    <input name="name" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">所属部门：</label>
                <div class="col-sm-8">
                    <select name="deptId" th:with="type=${@dict.getDept()}"class="form-control m-b" >
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>

<!--                <div class="col-sm-8">-->
<!--                    <input name="deptId" class="form-control" type="text">-->
<!--                </div>-->
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">卡号：</label>
                <div class="col-sm-8">
                    <input id="authentication"  name="kaHao" class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "baozhi/employee1"
        $("#form-employee1-add").validate({
            focusCleanup: true
        });

        function websocketScanRead (callback) {
            if (typeof callback !== "function") {
                Toast.error('请设置回调函数。');
                return;
            }

            var ws = new WebSocket("ws://127.0.0.1:8181");
            ws.onopen = function (e) {
                console.log('扫描枪websocket链接成功。');

                var Jsonobj = {"name":"GB2312","Type":"tt1"};
                ws.send(Jsonobj.name);
            };
            ws.onmessage = function (e) {
                //console.log("扫描枪websocket接收到数据");
                //console.log(e.data);
                try {
                    var obj=JSON.parse(e.data);
                    var result = obj.data;
                    //区分是否是扫描检索模式及扫描登记模式
                    if (!!result && result.length > 5 && result != "GB2312") {
                        result = result.substring(0,result.length-1);
                        callback(obj.command, result);
                    } else {
                        //Toast.warning("未获取扫描数据!");
                    }
                } catch (e) {
                    console.log(e);
                }
            };
            ws.onerror = function (e) {
                console.log("扫描枪websocket错误");
                scanCloseWebsocket(ws);
            };
            ws.onclose = function (e) {
                console.log("扫描枪websocket关闭连接");
            };

            function scanCloseWebsocket(ws) {
                try {
                    ws.close();
                } catch (e) {
                    console.log(e);
                }
            }
        }

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-employee1-add').serialize());
            }
        }
        $(function() {
            //扫描登记
            websocketScanRead(function (command, barcode) {
                $("#codebar").val(barcode);
                if(barcode){
                    if(command=="CardNo"){
                        //读卡器
                        $("#authentication").val(barcode);
                    }
                }
            });

        });
    </script>
</body>
</html>